package com.blueocean_health.care.management.handle;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import com.blueocean_health.care.management.common.base.string.StringUtils;

/**
* @author huyanqiu
* @version 2017年8月22日 下午4:14:37
*/
@MappedJdbcTypes({JdbcType.VARCHAR})
@MappedTypes({String[].class})
public class Array2StrHandle extends BaseTypeHandler<String[]> {

	@Override  
    public String[] getNullableResult(ResultSet rs, String columnName)  
           throws SQLException {  
       return getStringArray(rs.getString(columnName));  
    }  
   
    @Override  
    public String[] getNullableResult(ResultSet rs, int columnIndex)  
           throws SQLException {  
       return this.getStringArray(rs.getString(columnIndex));  
    }  
   
    @Override  
    public String[] getNullableResult(CallableStatement cs, int columnIndex)  
           throws SQLException {  
       return this.getStringArray(cs.getString(columnIndex));  
    }  
   
    @Override  
    public void setNonNullParameter(PreparedStatement ps, int i,  
           String[] parameter, JdbcType jdbcType) throws SQLException {  
       //由于BaseTypeHandler中已经把parameter为null的情况做了处理，所以这里我们就不用再判断parameter是否为空了，直接用就可以了  
       StringBuffer result = new StringBuffer();  
       if (!StringUtils.isEmpty(parameter)) {
    	   for (String value : parameter) {
               result.append(value).append(",");
    	   }
	       result.deleteCharAt(result.length() - 1);
	       ps.setString(i, result.toString());
           
       } else {
    	   ps.setString(i, "");
       }
    }  
     
    private String[] getStringArray(String columnValue) {  
       if (columnValue == null || "".equals(columnValue)) {
    	   return new String[] {};
       } 
       return columnValue.split(",");  
    }  

}
